#ifndef TEMPCTRLIB_H_
#define TEMPCTRLIB_H_

#include <stdint.h>
#include "dataproc.h"
#include "pid.h"
#include "mytype.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define PidCal(x) ind_pid(x)

	typedef enum
	{
		TLIB_STEP_INIT = 0,
		TLIB_STEP_COOL,
		TLIB_STEP_HEAT,
		TLIB_STEP_AUTO,
	} tlib_ctr_step_type;

	typedef enum
	{
		TLIB_DIR_DOWN = 0,
		TLIB_DIR_UP = 1,
	} tlib_dir_type;
	
	typedef enum
	{
		COOL_MODE_OFF = 0x00,	   
		COOL_MODE_ON = 0x01,	   
		COOL_MODE_AUTO_FIX = 0x10, 
		COOL_MODE_AUTO_MIN = 0x11, 
		COOL_MODE_AUTO_MAX = 0x12, 
	} tlib_cool_mode_type;

	typedef enum
	{
		COOL_SW_MODE_NONE = 0,
		COOL_CDIS_AT_DIR_DWON = 1,
		COOL_CEN_AT_DLY_END = 2,
	} cool_sw_mode;

	typedef struct
	{
		lib_ot2_type heat_on; 
		state_req_type cool_on;	  
		int32_t heat_out; 
	} tlib_manual_type;
	
	typedef struct
	{
		uint32_t time_cnt;		
		uint32_t step_time_cnt; 
	} tlib_common_par_type;

	typedef struct
	{
		int16_t temp_protect;	  
		int16_t dly_time_sv;	  
		int16_t quit_time_sv;	  
	} tlib_cool_cfg_type;

	typedef struct
	{
		uint8_t wave_cnt;			  
		cool_sw_mode st_cool_sw_mode; 
									  
		lib_ot2_type st_cool_req;		  
		lib_ot2_type st_cool_req_old;	  
		
		int16_t cool_stop_time;		  
		
		int16_t cool_run_time;		  
	} tlib_cool_state_type;

	typedef struct
	{
		tlib_cool_mode_type cool_mode; 
		lib_st2_type st_heat_dis; 
		int16_t pv;			  
		int16_t sv;			  
		int16_t cool_en;	  
		int16_t cool_dis;	  
		int16_t ctr_lmt_high; 
		int16_t ctr_lmt_low;  
		float f_sv;			  
	} tlib_object_cfg_type;

	typedef struct
	{
		tlib_ctr_step_type ctr_step;	 
		int16_t pv_max, pv_max_old;					 
		int16_t pv_min, pv_min_old;					 
		uint8_t back_cnt;				 
		tlib_dir_type st_dir;			 
		tlib_dir_type st_dir_old;		 
		tlib_dir_type st_dir_at_dly_end; 
		tlib_ctr_step_type ctr_step_old; 
		lib_ot2_type st_cool_req;			 
		lib_st2_type st_edge_cool_delay_end; 
		lag16_type lag;					 
		int16_t pv_lag;					 
		int16_t pv_for_dir;				 
		int16_t pv_cool_dly_end;		 
		int16_t pv_cool_start;			 
		int16_t pv_cool_end;			 
		int16_t diff_dis_to_dly_end;	 
										 
		int16_t sv_old;					 
		int16_t sv_back;				 
		int32_t heat_out;				 
		pid_type pid_obj;				 
	} tlib_object_state_type;

	typedef struct
	{
		tlib_manual_type manual;
		tlib_cool_cfg_type cool_cfg;
		tlib_cool_state_type cool_st;
		tlib_object_cfg_type temp_cfg;
		tlib_object_state_type temp_st;
		tlib_common_par_type common;
	} temp_box_ctr_type;

	void temp_box_ctr(temp_box_ctr_type *TempCtrPar); 

	void tlib_temp_cool_adj_min(
		tlib_cool_cfg_type *cool_cfg,
		tlib_cool_state_type *cool_st,
		tlib_object_cfg_type *object_cfg,
		tlib_object_state_type *object_st);

	void tlib_temp_cool_adj_max(
		tlib_cool_cfg_type *cool_cfg,
		tlib_cool_state_type *cool_st,
		tlib_object_cfg_type *object_cfg,
		tlib_object_state_type *object_st);

	void tlib_temp_ctr_init(tlib_cool_cfg_type *cool_cfg, tlib_cool_state_type *cool_st,
							tlib_object_cfg_type *object_cfg, tlib_object_state_type *object_st);
	void tlib_temp_ctr_cool(tlib_cool_cfg_type *cool_cfg, tlib_cool_state_type *cool_st,
							tlib_object_cfg_type *object_cfg, tlib_object_state_type *object_st);
	void tlib_temp_ctr_heat(tlib_object_cfg_type *object_cfg, tlib_object_state_type *object_st);
	void tlib_temp_ctr_auto(
		tlib_common_par_type *common,
		tlib_cool_cfg_type *cool_cfg,
		tlib_object_cfg_type *object_cfg, tlib_object_state_type *object_st);

	void tlib_temp_box_ctr_temp_dir(tlib_object_state_type *object_st);
	void tlib_temp_pid_run(tlib_object_state_type *object_st);

#ifdef __cplusplus
}
#endif

#endif 
